package com.phoenix.sgs.faq.general;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.phoenix.sgs.faq.db.DBHelper;
import com.phoenix.sgs.faq.meta.PublishPackage;

public class General {
	public General(int id, String code, String name, String alias,
			PublishPackage publishPackage, Country country, int gender,
			int blood, boolean isKing, boolean isGod) {
		super();
		this.id = id;
		this.code = code;
		this.name = name;
		this.alias = alias;
		this.publishPackage = publishPackage;
		this.country = country;
		this.gender = gender;
		this.blood = blood;
		this.isKing = isKing;
		this.isGod = isGod;
	}
	
	public General(Context context, int id) {
		this.context = context;
		SQLiteDatabase db = this.getDatabase();
		Cursor cursor = null;
		try {
			String sql = "SELECT g.*, p.Name as PackageName, p.IsOfficial, c.Name as CountryName " +
				"FROM Generals g, Packages p, Countries c " +
				"WHERE g.PackageId = p.Id " +
				"AND g.CountryId = c.Id " +
				"AND g.Id = " + String.valueOf(id);
			cursor = db.rawQuery(sql, null);
			if (cursor == null) {
				throw new Exception("Query General data error.");
			}
		    
			int numColumn_id = cursor.getColumnIndex("Id");
			int numColumn_code = cursor.getColumnIndex("Code");
			int numColumn_name = cursor.getColumnIndex("Name");
			int numColumn_alias = cursor.getColumnIndex("Alias");
			int numColumn_gender = cursor.getColumnIndex("Gender");
			int numColumn_blood = cursor.getColumnIndex("Blood");
			int numColumn_isking = cursor.getColumnIndex("IsKing");
			int numColumn_isgod = cursor.getColumnIndex("IsGod");
			int numColumn_packageid = cursor.getColumnIndex("PackageId");
			int numColumn_packagename = cursor.getColumnIndex("PackageName");
			int numColumn_isofficial = cursor.getColumnIndex("IsOfficial");
			int numColumn_countryid = cursor.getColumnIndex("CountryId");
			int numColumn_countryname = cursor.getColumnIndex("CountryName");
			
			if (cursor.moveToFirst()) {
				this.id = cursor.getInt(numColumn_id);
				this.code = cursor.getString(numColumn_code).trim();
				this.name = new String(cursor.getBlob(numColumn_name), "GBK").trim();
				this.alias = new String(cursor.getBlob(numColumn_alias), "GBK").trim();
				this.publishPackage = new PublishPackage(
						cursor.getInt(numColumn_packageid),
						new String(cursor.getBlob(numColumn_packagename), "GBK").trim(),
						cursor.getInt(numColumn_isofficial) > 0);
				this.country = new Country(
						cursor.getInt(numColumn_countryid),
						new String(cursor.getBlob(numColumn_countryname), "GBK").trim());
				this.gender = cursor.getInt(numColumn_gender);
				this.blood = cursor.getInt(numColumn_blood);
				this.isKing = cursor.getInt(numColumn_isking) > 0;
				this.isGod = cursor.getInt(numColumn_isgod) > 0;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
	}
	
	private Context context;
	private int id;
	private String code;
	private String name;
	private String alias;
	private PublishPackage publishPackage;
	private Country country;
	private int gender;
	private int blood;
	private boolean isKing;
	private boolean isGod;
	private List<Skill> skills = null;
	
	public List<Skill> getSkills() {
		if (this.skills == null) {
			this.loadSkills();
		}
		
		return skills;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAlias() {
		return alias;
	}
	public void setAlias(String alias) {
		this.alias = alias;
	}
	public PublishPackage getPublishPackage() {
		return publishPackage;
	}
	public void setPublishPackage(PublishPackage publishPackage) {
		this.publishPackage = publishPackage;
	}
	public Country getCountry() {
		return country;
	}
	public void setCountry(Country country) {
		this.country = country;
	}
	public int getGender() {
		return gender;
	}
	public void setGender(int gender) {
		this.gender = gender;
	}
	public int getBlood() {
		return blood;
	}
	public void setBlood(int blood) {
		this.blood = blood;
	}
	public boolean isKing() {
		return isKing;
	}
	public void setKing(boolean isKing) {
		this.isKing = isKing;
	}
	public boolean isGod() {
		return isGod;
	}
	public void setGod(boolean isGod) {
		this.isGod = isGod;
	}
	
	private SQLiteDatabase getDatabase() {
		DBHelper dbHelper = new DBHelper(this.context);
    	SQLiteDatabase db = dbHelper.openDatabase();
    	return db;
	}
	
	private void loadSkills() {
		SQLiteDatabase db = this.getDatabase();
		Cursor cursor = null;
		List<Skill> result = new ArrayList<Skill>();
		
		try {
			String sql = "SELECT s.*, st.Name as SkillTypeName " +
				"FROM Skills s, SkillTypes st " +
				"WHERE s.SkillTypeId = st.Id " +
				"AND s.GeneralId = " + this.getId();
			cursor = db.rawQuery(sql, null);
			if (cursor == null) {
				throw new Exception("Query  data error.");
			}
		    
			int numColumn_id = cursor.getColumnIndex("Id");
			int numColumn_typeid = cursor.getColumnIndex("SkillTypeId");
			int numColumn_typename = cursor.getColumnIndex("SkillTypeName");
			int numColumn_name = cursor.getColumnIndex("Name");
			int numColumn_desc = cursor.getColumnIndex("Description");
			int numColumn_comments = cursor.getColumnIndex("Comments");
			
			if (cursor.moveToFirst()) {
				do {
					Skill skill = new Skill();
					skill.setId(cursor.getInt(numColumn_id));
					skill.setGeneralId(this.getId());
					skill.setSkillType(new SkillType(
							cursor.getInt(numColumn_typeid), 
							new String(cursor.getBlob(numColumn_typename), "GBK").trim()));
					skill.setName(
							new String(cursor.getBlob(numColumn_name), "GBK").trim());
					skill.setDescription(
							new String(cursor.getBlob(numColumn_desc), "GBK").trim());
					if(!cursor.isNull(numColumn_comments)) {
						skill.setComments(
							new String(cursor.getBlob(numColumn_comments), "GBK").trim());
					}
					result.add(skill);
				} while (cursor.moveToNext());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		
		this.skills = result;
	}
}
